package com.onpositive.funkytable.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.IReadableObjectCollection;

public class SpecPartsBuilder {

	public static ArrayList<SpecificationPart> createSpecParts(
			final IReadableObjectCollection<Object> collection,
			final int parseInt) {
		ArrayList<PropertyValue> actual = getValuedPropertyList(collection,
				parseInt);
		Collections.sort(actual, new Comparator<PropertyValue>() {

			@Override
			public int compare(PropertyValue lhs, PropertyValue rhs) {
				String group = FieldWithMeta.getLayoutGroup(lhs.property,lhs.value);
				String group1 = FieldWithMeta.getLayoutGroup(rhs.property,rhs.value);
				return group.compareTo(group1);
			}
		});
		SpecificationPart current=null;
		ArrayList<SpecificationPart>specParts=new ArrayList<SpecificationPart>();
		ArrayList<SpecificationPart>specMParts=new ArrayList<SpecificationPart>();
		SpecificationPart general=new SpecificationPart();
		general.title="General";
		for (PropertyValue m : actual) {
			Object value=m.value;
			if (value instanceof String) {
				String s = (String) value;
				if (s.indexOf("*#*") != -1) {
					String[] rl = s.split("\\\\*#\\\\*");
					for (int a = 0; a < rl.length; a++) {
						if (rl[a].charAt(rl[a].length() - 1) == '*') {
							rl[a] = rl[a].substring(0,
									rl[a].length() - 1);
						}
						if (rl[a].charAt(0) == '*') {
							rl[a] = rl[a].substring(1);
						}
					}
					m.value=new ValueSequence(rl);
				}				
			}
			if (value instanceof byte[]) {
				byte[] vl = (byte[]) value;
				m.value=vl;
			}
			if (value instanceof Double){
				Object value2 = m.property.getMetaPropertyValue("unit");
				if (value2!=null){
					m.value=new Object[]{value2,value};
				}
			}
			String id = m.property.id();
			if (id.equals("crew")||id.equals("designer")||id.equals("number built")||id.equals("status")||id.equals("primary user")||id.equals("manufacturer")||id.equals("type")||id.equals("general")){
				general.properties.add(m);
				continue;
			}
			if (m.value instanceof ValueSequence){
				SpecificationPart t = new SpecificationPart();
				t.title = m.property.id();
				ValueSequence s=(ValueSequence) m.value;
				for (Object o:s.sequence){
					PropertyValue propertyValue = new PropertyValue(m.property, o);
					propertyValue.needsLabel=false;
					t.properties.add(propertyValue);
				}
				specMParts.add(t);
				continue;
			}
			
			if (current == null
					|| !current.title.equals(FieldWithMeta.getLayoutGroup(m.property,m.value))) {
				if (current!=null){
					specParts.add(current);
				}
				SpecificationPart t = new SpecificationPart();
				t.title = FieldWithMeta.getLayoutGroup(m.property,m.value);
				current=t;
			}
			current.properties.add(m);
		}
		if (current!=null){
			specParts.add(current);
		}
		specParts.addAll(specMParts);
		if (general.properties.size()>0){
			specParts.add(0, general);	
		}
		for (SpecificationPart p:specParts){
			if (p.title.equals("kg")){
				p.title="Weight&Loading";
			}
			else if (p.title.equals("km/h")){
				p.title="Speed";
			}
			else if (p.title.equals("date")){
				p.title="Notable dates";
			}
			else if (p.title.equals("W")){
				p.title="Engine&Power";
			}
			else if (p.title.equals("value sequence")){
				p.title="Other";
			}
			else{
				p.title=Character.toUpperCase(p.title.charAt(0))+p.title.substring(1);
			}
			Collections.sort(p.properties,new Comparator<PropertyValue>() {

				@Override
				public int compare(PropertyValue lhs, PropertyValue rhs) {
					String group = FieldWithMeta.getGroup(lhs.property);
					String group1 = FieldWithMeta.getGroup(rhs.property);
					int r=group.compareTo(group1);
					if (r!=0){
						return -r;
					}
					return lhs.property.id().compareTo(rhs.property.id());
				}
			});
		}
				
		return specParts;
	}
	
	public static ArrayList<PropertyValue> getValuedPropertyList(
			final IReadableObjectCollection<Object> collection,
			final int parseInt) {
		ArrayList<PropertyValue> actual = new ArrayList<PropertyValue>();
		Collection<IPropertyMetadata> properties = collection.properties();
		for (IPropertyMetadata m : properties) {
			if (m.id().equals(
					IPropertyConstants.MAIN_PICTURE_PROPERTY_METADATA_ID)) {
				continue;
			}
			if (m.id().equals(
					IPropertyConstants.PREVIEW_PICTURE_PROPERTY_METADATA_ID)) {

				continue;
			}
			if (m.id().equals("files")) {
				continue;
			}
			if (m.id().equals("caption")) {
				continue;
			}
			if (m.id().equals("last")) {
				continue;
			}
			if (m.id().equals("date")) {
				continue;
			}
			if (m.id().equals("name")) {
				continue;
			}
			if (m.id().equals("image")) {
				continue;
			}
			if (m.id().equals("url")) {
				continue;
			}
			if (m.id().equals(IPropertyConstants.PICTURE_LIST_PROPERTY_NAME)) {
				continue;
			}
			if (m.id().equals(
					IPropertyConstants.PARENT_CATEGORIES_PROPERTY_NAME)) {
				continue;
			}

			if (m.id().equals(IPropertyConstants.TEXT_CONTENT_PROPERTY_NAME)) {
				continue;
			}
			Object value = collection.getValue(parseInt, m);
			if (m.id().equals(
					IPropertyConstants.DOCUMENT_NAME_PROPERTY_METADATA_ID)) {
				continue;
			}

			if (value != null && value.toString().length() > 0) {
				actual.add(new PropertyValue(m, value));
			}
		}
		return actual;
	}
}
